package com.hyd.pachosting;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@Slf4j // 添加 Slf4j 注解
public class IndexController {

    @Autowired
    private JdbcTemplate jdbcTemplate; // 注入 JdbcTemplate

    @GetMapping("/")
    public String home(HttpSession session) {
        if (session.getAttribute("user") == null) {
            return "redirect:/login";
        }
        return "home";  // 需要创建templates/home.html
    }

    @GetMapping("/login")
    public String loginForm() {
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username,
                       @RequestParam String password,
                       HttpSession session) {
        try {
            // 查询数据库验证用户
            String sql = "SELECT COUNT(*) FROM t_user WHERE username = ? AND password = ?";
            Integer userCount = jdbcTemplate.queryForObject(sql, Integer.class, username, password);

            if (userCount > 0) {
                session.setAttribute("user", username);
                return "redirect:/";
            } else {
                return "redirect:/login?error";
            }
        } catch (Exception e) {
            log.error("登录过程中发生错误: {}", e.getMessage(), e); // 使用 log.error 替代 e.printStackTrace()
            return "redirect:/login?error";
        }
    }

    // 新增：注册页面
    @GetMapping("/register")
    public String registerForm() {
        return "register";
    }

    // 新增：处理注册请求
    @PostMapping("/register")
    public String register(@RequestParam String username,
                           @RequestParam String password,
                           HttpSession session) {
        try {
            // 检查用户名是否已存在
            String checkSql = "SELECT COUNT(*) FROM t_user WHERE username = ?";
            Integer userCount = jdbcTemplate.queryForObject(checkSql, Integer.class, username);
            
            if (userCount > 0) {
                return "redirect:/register?error"; // 用户名已存在，跳转回注册页面并显示错误信息
            }

            // 插入新用户
            String insertSql = "INSERT INTO t_user (username, password) VALUES (?, ?)";
            int rowsAffected = jdbcTemplate.update(insertSql, username, password);

            if (rowsAffected > 0) {
                session.setAttribute("user", username);  // 注册成功后自动登录
                return "redirect:/";
            } else {
                return "redirect:/register?error";
            }
        } catch (Exception e) {
            log.error("注册过程中发生错误: {}", e.getMessage(), e); // 使用 log.error 替代 e.printStackTrace()
            return "redirect:/register?error";
        }
    }
}